package com.hx.voice.recognition.common.context;

import com.hx.voice.recognition.common.constant.CommonConstants;
import com.hx.voice.recognition.common.util.StringHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
 * @descripe：应用变量上下文
 * @company：南京华讯方舟通讯设备有限公司
 * @author：zhoujinbing
 * @time：2019/6/11 15:47
 * @version：V1.0
 */
public class BaseContextHandler {

    public static  ThreadLocal<Map<String,Object>> threadLocal = new ThreadLocal<>();

    /**
     * @description 变量池中存放key、value
     * @method  set
     * @param  [key, value]
     * @return void
     * @date: 2019/6/11 15:50
     * @author:zhoujinbing
     */
    public static void set(String key,Object value)
    {
        Map<String, Object> stringObjectMap = threadLocal.get();
        if(stringObjectMap == null)
        {
            stringObjectMap = new HashMap<>();
            threadLocal.set(stringObjectMap);
        }
        stringObjectMap.put(key,value);

    }

    /**
     * @description 变量池中取value
     * @method  get
     * @param  [key]
     * @return void
     * @date: 2019/6/11 15:50
     * @author:zhoujinbing
     */
    public static Object get(String key)
    {
        Map<String, Object> stringObjectMap = threadLocal.get();
        if(stringObjectMap == null)
        {
            stringObjectMap = new HashMap<>();
            threadLocal.set(stringObjectMap);
        }
        return stringObjectMap.get(key);
    }

    public static String getUserID()
    {
        Object value = get(CommonConstants.CONTEXT_KEY_USER_ID);
        return returnObjectValue(value);
    }

    public static String getUsername()
    {
        Object value = get(CommonConstants.CONTEXT_KEY_USERNAME);
        return returnObjectValue(value);
    }

    public static String getName()
    {
        Object value = get(CommonConstants.CONTEXT_KEY_USER_NAME);
        return StringHelper.getObjectValue(value);
    }

    public static String getToken()
    {
        Object value = get(CommonConstants.CONTEXT_KEY_USER_TOKEN);
        return StringHelper.getObjectValue(value);
    }

    public static void setToken(String token)
    {
        set(CommonConstants.CONTEXT_KEY_USER_TOKEN,token);
    }

    public static void setName(String name)
    {
        set(CommonConstants.CONTEXT_KEY_USER_NAME,name);
    }

    public static void setUserID(String userID)
    {
        set(CommonConstants.CONTEXT_KEY_USER_ID,userID);
    }

    public static void setUsername(String username)
    {
        set(CommonConstants.CONTEXT_KEY_USERNAME,username);
    }

    private static String returnObjectValue(Object value)
    {
        return value == null ? null : value.toString();
    }

    public static void remove()
    {
        threadLocal.remove();
    }

    @RunWith(MockitoJUnitRunner.class)
    public static class UnitTest {
        private Logger logger = LoggerFactory.getLogger(UnitTest.class);

        @Test
        public void testSetContextVariable() throws InterruptedException {
            BaseContextHandler.set("test", "main");
            new Thread(() -> {
                BaseContextHandler.set("test", "moo");

                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                assertEquals(BaseContextHandler.get("test"), "moo");
                logger.info("thread one done!");
            }).start();
            new Thread(() -> {
                BaseContextHandler.set("test", "moo2");
                assertEquals(BaseContextHandler.get("test"), "moo2");
                logger.info("thread two done!");
            }).start();

            Thread.sleep(5000);
            assertEquals(BaseContextHandler.get("test"), "main");
            logger.info("main one done!");
        }

        @Test
        public void testSetUserInfo() {
            BaseContextHandler.setUserID("test");
            assertEquals(BaseContextHandler.getUserID(), "test");
            BaseContextHandler.setUsername("test2");
            assertEquals(BaseContextHandler.getUsername(), "test2");
        }
    }

}